<template>
  <div class="app-container">
    <el-button type="primary" @click="handleAdd">创建用户</el-button>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="编号" width="95">
        <template slot-scope="scope">{{ scope.$index }}</template>
      </el-table-column>
      <el-table-column align="center" label="用户名">
        <template slot-scope="scope">{{ scope.row.user_name }}</template>
      </el-table-column>
      <el-table-column align="center" label="用户权限" min-width="110">
        <template slot-scope="scope">{{ scope.row.user_role | formatRole }}</template>
      </el-table-column>
      <el-table-column align="center" prop="createtime" label="创建时间" min-width="200">
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span>{{ scope.row.createtime }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="updatetime" label="更新时间" min-width="200">
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span>{{ scope.row.updatetime }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" prop label="操作" width="200">
        <template slot-scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :visible.sync="dialogTableVisible" title="Shipping address">
      <el-form ref="form" :model="form" label-width="120px">
        <el-form-item label="用户名">
          <el-input v-model="userForm.user_name" />
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="userForm.user_pwd" />
        </el-form-item>
        <el-form-item label="权限">
          <el-radio-group v-model="userForm.user_role">
            <el-radio label="2">普通用户</el-radio>
            <el-radio label="1">管理员</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">Create</el-button>
          <el-button @click="onCancel">Cancel</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { getList } from "@/api/table";
import { getUserList, addUser, updateUser, deleteUser } from "@/api/user";

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: "success",
        draft: "gray",
        deleted: "danger"
      };
      return statusMap[status];
    }
  },
  data() {
    return {
      list: null,
      listLoading: true,
      dialogTableVisible: false,
      userForm: {}
    };
  },
  created() {
    this.fetchData();
  },
  methods: {
    fetchData() {
      this.listLoading = true;
      getUserList().then(response => {
        this.list = response;
        this.listLoading = false;
      });
      // this.list = [
      //   {
      //     user_id: 2,
      //     user_name: "adadad",
      //     user_pwd: "adadad",
      //     user_role: "1",
      //     createtime: "1231",
      //     updatetime: "13131"
      //   },
      //   {
      //     user_id: 6,
      //     user_name: "王1",
      //     user_pwd: "123456",
      //     user_role: "2",
      //     createtime: "1231",
      //     updatetime: "13131"
      //   },
      //   {
      //     user_id: 4,
      //     user_name: "李四",
      //     user_pwd: "123456",
      //     user_role: "2",
      //     createtime: "1231",
      //     updatetime: "13131"
      //   },
      //   {
      //     user_id: 5,
      //     user_name: "王五",
      //     user_pwd: "123456",
      //     user_role: "2",
      //     createtime: "1231",
      //     updatetime: "13131"
      //   }
      // ];
      // this.listLoading = false;
    },
    handleAdd() {
      this.userForm = {}
      this.dialogTableVisible = true;
    },
    //显示编辑界面
    handleEdit: function(index, row) {
      this.dialogTableVisible = true;
      this.userForm = Object.assign({}, row);
    },
    //删除
    handleDel: function(index, row) {
      this.$confirm("确认删除该记录吗?", "提示", {
        type: "warning"
      })
        .then(() => {
          this.listLoading = true;
          //NProgress.start();
          deleteUser(row.idNo).then(res => {
            // this.listLoading = false;
            if (res.state === 200) {
              this.$message({
                message: "删除成功",
                type: "success"
              });
              this.fetchData();
            } else {
              this.$message({
                message: "删除失败",
                type: "warning"
              });
            }
          });
        })
        .catch(() => {});
    },
    onSubmit() {
      if (this.userForm.user_id) {
        updateUser(this.userForm).then(response => {
          if (res.state === 200) {
            this.$message({
              message: "添加成功",
              type: "success"
            });
          } else {
            this.$message({
              message: "添加失败",
              type: "warning"
            });
          }
        });
      } else {
        addUser(this.userForm).then(response => {
          if (res.state === 200) {
            this.$message({
              message: "修改成功",
              type: "success"
            });
          } else {
            this.$message({
              message: "修改失败",
              type: "warning"
            });
          }
        });
      }
    },
    onCancel() {
      this.$message({
        message: "cancel!",
        type: "warning"
      });
    }
  },
		filters: {
			formatRole: function (roleId) {
        if (Number(roleId) === 1) {
          return "管理员"
        } else {
          return "普通用户"
        }
			}
		}
};
</script>
